package com.ruoyi.commentmanage.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.commentmanage.mapper.CmCommentMapper;
import com.ruoyi.commentmanage.service.ICmCommentService;
import com.ruoyi.commentmanage.domain.CmComment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.useradmin.service.IUaUserService;
import com.ruoyi.activieadmin.service.IAaActiveService;
import com.ruoyi.campadmin.service.ICaAmenityService;
import com.ruoyi.campadmin.service.ICaItemRentalService;
import com.ruoyi.campadmin.service.ICaItemSaleService;
import com.ruoyi.campadmin.service.ICaVenueRentalService;
import com.ruoyi.traveladmin.service.ITaTravelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CmCommentServiceImpl implements ICmCommentService {


    @Resource
    private CmCommentMapper cmCommentMapper;
    
    
    @Resource
    private IUaUserService uaUserService;

    //评论类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
    //1
    @Resource
    private ICaVenueRentalService caVenueRentalService;
    //2
    @Resource
    private ICaItemRentalService caItemRentalService;
    //3
    @Resource
    private ICaItemSaleService caItemSaleService;
    //4
    @Resource
    private ICaAmenityService caAmenityService;
    //5
    @Resource
    private IAaActiveService aaActiveService;
    //6
    @Resource
    private ITaTravelService taTravelService;


    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param cmComment 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CmComment> getCmCommentPage(PageDomain pageDomain, CmComment cmComment) {
        if (Objects.isNull(cmComment)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CmComment> queryWrapper = new LambdaQueryWrapper<>(new CmComment());
            //if(!Objects.isNull(cmComment.getName())){
            //	queryWrapper.like(CmComment::getName,cmComment.getName());
            //}
            

            if (!Objects.isNull(cmComment.getId())) {
                queryWrapper.eq(CmComment::getId, cmComment.getId());
            }

            if (!Objects.isNull(cmComment.getUaUserId())) {
                queryWrapper.eq(CmComment::getUaUserId, cmComment.getUaUserId());
            }

            if (!Objects.isNull(cmComment.getCommentType())) {
                queryWrapper.like(CmComment::getCommentType, cmComment.getCommentType());
            }

            if (!Objects.isNull(cmComment.getCommentTypeId())) {
                queryWrapper.eq(CmComment::getCommentTypeId, cmComment.getCommentTypeId());
            }

            if (!Objects.isNull(cmComment.getCommentText())) {
                queryWrapper.like(CmComment::getCommentText, cmComment.getCommentText());
            }

            if (!Objects.isNull(cmComment.getPraiseNum())) {
                queryWrapper.like(CmComment::getPraiseNum, cmComment.getPraiseNum());
            }

            if (!Objects.isNull(cmComment.getFollowNum())) {
                queryWrapper.like(CmComment::getFollowNum, cmComment.getFollowNum());
            }

            if (!Objects.isNull(cmComment.getStatus())) {
                queryWrapper.like(CmComment::getStatus, cmComment.getStatus());
            }

            if (!Objects.isNull(cmComment.getDelFlag())) {
                queryWrapper.like(CmComment::getDelFlag, cmComment.getDelFlag());
            }

            if (!Objects.isNull(cmComment.getCreateBy())) {
                queryWrapper.like(CmComment::getCreateBy, cmComment.getCreateBy());
            }

            if (!Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeBeginTime"), cmComment.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmComment.getUpdateBy())) {
                queryWrapper.like(CmComment::getUpdateBy, cmComment.getUpdateBy());
            }

            if (!Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeBeginTime"), cmComment.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmComment.getRemark())) {
                queryWrapper.like(CmComment::getRemark, cmComment.getRemark());
            }
        Page<CmComment> cmCommentList= cmCommentMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CmComment record:cmCommentList.getRecords()){
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
                        //评论类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
            switch (record.getCommentType()){
                case "1":
                    record.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(record.getCommentTypeId()));
                    break;
                case "2":
                    record.setCaItemRental(caItemRentalService.getCaItemRentalById(record.getCommentTypeId()));
                    break;
                case "3":
                    record.setCaItemSale(caItemSaleService.getCaItemSaleById(record.getCommentTypeId()));
                    break;
                case "4":
                    record.setCaAmenity(caAmenityService.getCaAmenityById(record.getCommentTypeId()));
                    break;
                case "5":
                    record.setAaActive(aaActiveService.getAaActiveById(record.getCommentTypeId()));
                    break;
                case "6":
                    record.setTaTravel(taTravelService.getTaTravelById(record.getCommentTypeId()));
                    break;
            }
        }

            return cmCommentList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CmComment getCmCommentById(Long id) {
        if (Objects.isNull(id)) {
            return new CmComment();
        }
        try {
            CmComment cmComment = cmCommentMapper.selectById(id);
            cmComment.setUaUser(uaUserService.getUaUserById(cmComment.getUaUserId()));
                        //评论类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
                        switch (    cmComment.getCommentType()){
                case "1":
                        cmComment.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(cmComment.getCommentTypeId()));
                    break;
                case "2":
                        cmComment.setCaItemRental(caItemRentalService.getCaItemRentalById(cmComment.getCommentTypeId()));
                    break;
                case "3":
                        cmComment.setCaItemSale(caItemSaleService.getCaItemSaleById(cmComment.getCommentTypeId()));
                    break;
                case "4":
                        cmComment.setCaAmenity(caAmenityService.getCaAmenityById(cmComment.getCommentTypeId()));
                    break;
                case "5":
                        cmComment.setAaActive(aaActiveService.getAaActiveById(cmComment.getCommentTypeId()));
                    break;
                case "6":
                        cmComment.setTaTravel(taTravelService.getTaTravelById(cmComment.getCommentTypeId()));
                    break;
            }
                        if (Objects.isNull(cmComment)){
                cmComment = new CmComment();
            }
            return cmComment;

        } catch (Exception e) {
            e.printStackTrace();
            return new CmComment();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param cmComment 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCmComment(CmComment cmComment) {
        if (Objects.isNull(cmComment)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CmComment> queryWrapper = new LambdaQueryWrapper<>(new CmComment());
            

            if (!Objects.isNull(cmComment.getId())) {
                queryWrapper.eq(CmComment::getId, cmComment.getId());
            }

            if (!Objects.isNull(cmComment.getUaUserId())) {
                queryWrapper.eq(CmComment::getUaUserId, cmComment.getUaUserId());
            }

            if (!Objects.isNull(cmComment.getCommentType())) {
                queryWrapper.like(CmComment::getCommentType, cmComment.getCommentType());
            }

            if (!Objects.isNull(cmComment.getCommentTypeId())) {
                queryWrapper.eq(CmComment::getCommentTypeId, cmComment.getCommentTypeId());
            }

            if (!Objects.isNull(cmComment.getCommentText())) {
                queryWrapper.like(CmComment::getCommentText, cmComment.getCommentText());
            }

            if (!Objects.isNull(cmComment.getPraiseNum())) {
                queryWrapper.like(CmComment::getPraiseNum, cmComment.getPraiseNum());
            }

            if (!Objects.isNull(cmComment.getFollowNum())) {
                queryWrapper.like(CmComment::getFollowNum, cmComment.getFollowNum());
            }

            if (!Objects.isNull(cmComment.getStatus())) {
                queryWrapper.like(CmComment::getStatus, cmComment.getStatus());
            }

            if (!Objects.isNull(cmComment.getDelFlag())) {
                queryWrapper.like(CmComment::getDelFlag, cmComment.getDelFlag());
            }

            if (!Objects.isNull(cmComment.getCreateBy())) {
                queryWrapper.like(CmComment::getCreateBy, cmComment.getCreateBy());
            }

            if (!Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(cmComment.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(cmComment.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CmComment::getCreateTime, cmComment.getParams().get("CreateTimeBeginTime"), cmComment.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmComment.getUpdateBy())) {
                queryWrapper.like(CmComment::getUpdateBy, cmComment.getUpdateBy());
            }

            if (!Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(cmComment.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(cmComment.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CmComment::getUpdateTime, cmComment.getParams().get("UpdateTimeBeginTime"), cmComment.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmComment.getRemark())) {
                queryWrapper.like(CmComment::getRemark, cmComment.getRemark());
            }

            List<CmComment> cmComments = cmCommentMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmComment cmCommentNo:cmComments){
                idList.add(cmCommentNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CmComment> queryWrapper = new LambdaQueryWrapper<>(new CmComment());
            queryWrapper.in(CmComment::getUaUserId, uaUserIdList);
            List<CmComment> cmComments = cmCommentMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmComment cmComment:cmComments){
                idList.add(cmComment.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }


    /**
     * 通过commentType外键idList查询idList
     *
     * @param commentTypeIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCommentTypeIdList(List<Long> commentTypeIdList) {
        if (Objects.isNull(commentTypeIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CmComment> queryWrapper = new LambdaQueryWrapper<>(new CmComment());
            queryWrapper.in(CmComment::getCommentTypeId, commentTypeIdList);
            List<CmComment> cmComments = cmCommentMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmComment cmComment:cmComments){
                idList.add(cmComment.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param cmComment 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCmComment(CmComment cmComment) {
        if (Objects.isNull(cmComment)) {
            return -1;
        }
        try {
        
            //cmComment.setSpell(PinYin.getPinYin(cmComment.getName()));
            return cmCommentMapper.insert(cmComment);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param cmComment 实体类
     * @return 结果
     */
    @Override
    public int updateCmCommentById(CmComment cmComment) {
        if (Objects.isNull(cmComment)) {
            return -1;
        }
        try {
            //cmComment.setSpell(PinYin.getPinYin(cmComment.getName()));
            return cmCommentMapper.updateById(cmComment);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCmCommentByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return cmCommentMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CmComment> getCmCommentList() {
        try {
            LambdaQueryWrapper<CmComment> queryWrapper = new LambdaQueryWrapper<>(new CmComment());
            queryWrapper.isNotNull(CmComment::getId);
            return cmCommentMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
